/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/
#include <QMessageBox>
#include <QLineEdit>
#include <QSqlError>
#include <QPushButton>
#include <QDirModel>
#include <QSettings>

#include "database.h"
#include "configurare.h"

#include "login.h"
#include "flipscrollview.h"
#include "kontamabilloader.h"
#include "baseinterface.h"

using namespace SQL;
using namespace KontAMabil;

Login::Login(QWidget* parent, Qt::WFlags fl)
		: KontAMabilWindow(parent, fl), Ui::loginForm(), m_configurare(0)
{
	setupUi(this);
	connect(intraButton, SIGNAL(clicked()), SLOT(intra()));
	connect(parola, SIGNAL(returnPressed()), SLOT(intra()));
	connect(configureazaButton, SIGNAL(clicked()), SLOT(configurare()));
	QSettings s;

	const QStringList & args=qApp->arguments();
	if (args.indexOf("--utilizator")!=-1 && args.size()>args.indexOf("--utilizator")+1)
		s.setValue("aplicatie/utilizator",args[args.indexOf("--utilizator")+1]);

	if (args.indexOf("--gazda")!=-1 && args.size()>args.indexOf("--gazda")+1)
		s.setValue("aplicatie/gazda",args[args.indexOf("--gazda")+1]);

	if (args.indexOf("--port")!=-1 && args.size()>args.indexOf("--port")+1)
		s.setValue("aplicatie/port",args[args.indexOf("--port")+1]);

	if (args.indexOf("--baza_de_date")!=-1 && args.size()>args.indexOf("--baza_de_date")+1)
		s.setValue("aplicatie/baza_de_date",args[args.indexOf("--baza_de_date")+1]);

	utilizator->setText(s.value("aplicatie/utilizator","postgres").toString());
	if (utilizator->text().length())
		parola->setFocus();
}

void Login::intra()
{
	QSettings s;
	QSqlDatabase db = defaultDatabase()->addDatabase("QPSQL");
	db.setUserName(utilizator->text());
	db.setPassword(parola->text());
	db.setDatabaseName(s.value("aplicatie/baza_de_date","kontamabil").toString());
	db.setHostName(s.value("aplicatie/gazda","localhost").toString());
	db.setPort(s.value("aplicatie/port",5432).toInt());
	if (!db.open())
		QMessageBox::critical(this, tr("Login"), tr(QString("Conectarea la baza de date a esuat generand mesajul:\n%1").arg(db.lastError().text()).toAscii()),QMessageBox::Ok);
	else
	{
		s.setValue("aplicatie/utilizator",utilizator->text());
		s.setValue("aplicatie/baza_de_date",db.databaseName());
		s.setValue("aplicatie/gazda",db.hostName());
		s.setValue("aplicatie/port",db.port());
		hide();
		if (kontAMabil()->basePlugins().size()<2)
		{
			QMessageBox::critical(this, tr("Login"), tr("Nu s-au incarcat toate modulele"),QMessageBox::Ok);
			close();
		}
		else
		{
			kontAMabil()->showNextBasePlugin();
			close();
		}
	}
}

void Login::configurare()
{
	if (!m_configurare)
	{
		m_configurare=new Configurare;
		connect(m_configurare, SIGNAL(aboutToReject()) , SLOT(show()));
		connect(m_configurare, SIGNAL(aboutToAccept()) , SLOT(show()));
	}
	hide();
	m_configurare->show();
}

